package cn.hr.service.impl;

import cn.hr.dao.mapper.ResumeMapper;
import cn.hr.pojo.Resume;
import cn.hr.service.ResumeService;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;

/**
 * 简历信息服务接口实现类
 *
 * @author MO
 */
@CacheConfig(cacheNames = "resume")
@Scope("prototype")
@Service("resumeService")
public class ResumeServiceImpl implements ResumeService {
    @Resource
    private ResumeMapper resumeMapper;

    @Override
    public boolean insert(Resume resume) throws Exception {
        //推荐面试,更改简历状态
        if (resume.getIsInterview()==1) resume.setStatus(2);
        //暂无应聘职位或者未推荐面试时简历状态为存档并去掉推荐信息
        if (resume.getPostId()==null||resume.getIsInterview()==0) {
            resume.setStatus(1);
            resume.setReferrer(null);
            resume.setReferrerDate(null);
            resume.setReferrerOpinion(null);
        }
        return resumeMapper.insert(resume)>0;
    }

    @Override
    public boolean update(Resume t) throws Exception {
        return resumeMapper.update(t)>0;
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        delAffix(id);
        return resumeMapper.delete(id)>0;
    }

    @Override
    public Resume get(Integer id) throws Exception {
        return resumeMapper.get(id);
    }


    @Override
    public PageInfo<Map> findListByCondition(Map m, int pageNo) throws Exception {
        PageHelper.startPage(pageNo, Constants.PAGESIZE);
        PageInfo<Map> pageInfo =new PageInfo<>(resumeMapper.findListByCondition(m));
        return pageInfo;
    }

    @Override
    public Long findCountByCondition(Map m) throws Exception {
        return resumeMapper.findCountByCondition(m);
    }

    @Override
    public boolean delAffix(Integer id) throws  Exception{
        Resume resume =get(id);
        if (resume.getAffixPath()!=null){
            File file =new File(resume.getAffixPath());
            if (file.exists()){
                file.delete();
            }
        }
        return resumeMapper.delAffix(id)>0;
    }

    @Override
    public boolean updPost(Integer id, Integer postId) throws Exception {
        return resumeMapper.updPost(id,postId)>0;
    }
}
